package leetcode.t2String;

/**
 * 13.罗马数字转整数
 * @author K
 * @date 2024/1/23 14:34
 */
public class T13_RomanToInt {

    public static int romanToInt(String s) {
        if (s.length() == 1) {
            return which(s.charAt(0));
        }
        int sum = 0;
        for (int i = 1; i < s.length(); i++) {
            int n1 = which(s.charAt(i - 1));
            int n2 = which(s.charAt(i));
            if (n1 >= n2)
                sum += n1;
            else
                sum -= n1;
        }
        sum += which(s.charAt(s.length() - 1));
        return sum;
    }

    public static int which(char ch) {
        switch(ch) {
            case 'I': return 1;
            case 'V': return 5;
            case 'X': return 10;
            case 'L': return 50;
            case 'C': return 100;
            case 'D': return 500;
            case 'M': return 1000;
            case 'a': return 4;
            case 'b': return 9;
            case 'c': return 40;
            case 'd': return 90;
            case 'e': return 400;
            case 'f': return 900;
        }
        return 0;
    }

    public static void main(String[] args) {
        int result = romanToInt("MCMXCIV");
        int result1 = romanToInt("LVIII");
        int result2 = romanToInt("IX");
        int result3 = romanToInt("IV");
        int result4 = romanToInt("III");

        System.out.println();

    }

}
